package com.ly.mp.biz.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.mp.biz.IWfProcessRuntimeBIZ;
import com.ly.mp.dal.comm.annotation.MpDataSource;
import com.ly.mp.dal.comm.jdbc.ReadWriteDataSourceDecision;
import com.ly.mp.entity.WfProcessRuntimeEntity;
import com.ly.mp.mapper.WfProcessRuntimeMapper;

@Service
public class WfProcessRuntimeBIZ extends ServiceImpl<WfProcessRuntimeMapper, WfProcessRuntimeEntity> implements IWfProcessRuntimeBIZ {
    private Logger log = LoggerFactory.getLogger(WfProcessRuntimeBIZ.class);
    
    @Autowired
    @Lazy
    private IWfProcessRuntimeBIZ processRuntimeBIZ;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    @MpDataSource(value = "default")
    public void testOne() {
        log.info("当前key: {} ",ReadWriteDataSourceDecision.getDsKey());
        WfProcessRuntimeEntity processRuntimeEntity = baseMapper.selectById("9ca78435e6db4321b99f679a070e6f45");
        log.info("写库: {}",processRuntimeEntity);
        
        ReadWriteDataSourceDecision.markRead("wbrs-test");
        processRuntimeBIZ.extracted();
    }
    
    @Transactional(rollbackFor = Exception.class)
    @MpDataSource(value = "wbrs-test",cascading = false)
    public void extracted() {
        log.info("当前key: {} ",ReadWriteDataSourceDecision.getDsKey());
        WfProcessRuntimeEntity selectById = baseMapper.selectById("f6542968289e45b9b021e1b30742441c");
        log.info("读库: {}",selectById);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @MpDataSource(value = "default")
    public void testTwo() {
        log.info("当前key: {} ",ReadWriteDataSourceDecision.getDsKey());
        WfProcessRuntimeEntity processRuntimeEntity = baseMapper.selectById("9ca78435e6db4321b99f679a070e6f45");
        log.info("写库: {}",processRuntimeEntity);
        
        ReadWriteDataSourceDecision.markWrite("wbrs-test");
        log.info("当前key: {} ",ReadWriteDataSourceDecision.getDsKey());
        WfProcessRuntimeEntity selectById = baseMapper.selectById("f6542968289e45b9b021e1b30742441c");
        log.info("读库: {}",selectById);
    }

    // 用atomikos框架之后 多数据源事务直接就好了, 后面就都不测试了
    @Override
    @Transactional(rollbackFor = Exception.class)
    @MpDataSource(value = "default")
    public void testThree() {
        log.info("当前key: {} ",ReadWriteDataSourceDecision.getDsKey());
        WfProcessRuntimeEntity processRuntimeEntity = baseMapper.selectById("9ca78435e6db4321b99f679a070e6f45");
        log.info("写库: {}",processRuntimeEntity);
        processRuntimeEntity.setProcessName(processRuntimeEntity.getProcessName()+"456");
        int updateById = baseMapper.updateById(processRuntimeEntity);
        log.info("更新结果: " + updateById);
        
        ReadWriteDataSourceDecision.markWrite("wbrs-test");
        log.info("当前key: {} ",ReadWriteDataSourceDecision.getDsKey());
        WfProcessRuntimeEntity selectById = baseMapper.selectById("f6542968289e45b9b021e1b30742441c");
        
        log.info("读库: {}",selectById);
        selectById.setProcessName(selectById.getProcessName()+"789");
        updateById = baseMapper.updateById(processRuntimeEntity);
        log.info("更新结果2: " + updateById);
        int a = 1/0;
        
    }
}
